﻿Ext.onReady(function(){
	
	var viewConfig = [{
			columnsText: '显示列',
			sortAscText: '升序',
			sortDescText: '降序'
		}];
	
	//出入库记录grid
	var sm = new Ext.grid.CheckboxSelectionModel({
		singleSelect : true
	});
	
	var tranKind = function(v) {
		if(v==0){
			return "供应商自付";
		}else if(v==1){
			return "我方垫付";
		}
	};
		
	 var dateConv = function(v){//日期转换
		if(!v)return;
		else{
			return new Date(v.time)
		}
	}
	
    var cmRecord = new Ext.grid.ColumnModel([sm,
		{header : '单据ID',		dataIndex : 'num',hidden:true},
		{header : '单据编号',		dataIndex : 'out_no'},
		{header : '发货部门',		dataIndex : 'uname'	}, 
		{header : '客  户',		dataIndex : 'prov_name'}, 
		{header : '验收日期',		dataIndex : 'operdate',renderer : Ext.util.Format.dateRenderer('Y-m-d')}, 
		{header : '操作员',		dataIndex : 'operName'},
		{header : '当时操作员部门',dataIndex : 'operUName',hidden:true},
		{header : '财务日期',		dataIndex : 'cw_date',renderer : Ext.util.Format.dateRenderer('Y-m-d')},
		{header : '运费',		dataIndex : 'tran_money'},
		{header : '支付方式',		dataIndex : 'tran_kind',renderer:tranKind},
		{header : '备注',		dataIndex : 'remark'},
		{header : '数据类型',		dataIndex : 'cw_flag',hidden:true}
	]);
	cmRecord.defaultSortable = true;

    var storeRecord = new Ext.data.Store({
        baseParams : {
			trancode : '51212',
			commitState:'1'
		},
		proxy : new Ext.data.HttpProxy({
			url : '../' + WebServiceUrl
		}),
		reader : new Ext.data.JsonReader({
			totalProperty : 'totalProperty',
			root : 'root'
		},  
        [{name : 'num'},
		{name : 'out_no'}, 
		{name : 'uname'}, 
		{name : 'prov_name'}, 
		{name : 'operdate',convert:dateConv},  //转换从后台传过来的DATE对象
		{name : 'operName'}, 
		{name : 'operUName'}, 
		{name : 'cw_date',convert:dateConv},
		{name : 'tran_money'}, 
		{name : 'tran_kind'}, 
		{name : 'remark'}, 
		{name : 'cw_flag'}
		])
    });

    var stockRecordGrid = new Ext.grid.GridPanel({
    	id:'stockRecordGrid',
		autoScroll: true,
		store: storeRecord,
		cm: cmRecord,
		border:false,
		sm: sm,
		tbar:[{
			xtype:'textfield',
			id:'condition',
			width:100,
			selectOnFocus : true
		},'-',{
			text:'搜索',
			iconCls : 'search',
			handler:doSearch
		},'-',{
			text:'新建',
			iconCls:'add',
			handler:function(){
				new StockEditor(false);
			}
		},'-',{
			text:'编缉',
			iconCls:'option',
			handler:function(){
				var record = Ext.getCmp('stockRecordGrid').getSelectionModel().getSelected();
				if(record)new StockEditor(record);
				else Ext.MessageBox.alert('操作错误','请先选择一条记录');
			}
		},'-',{
			text:'删除',
			iconCls : 'remove',
			handler:function(){
				var record = Ext.getCmp('stockRecordGrid').getSelectionModel().getSelected();
				if(record){
					Ext.Msg.confirm('确认删除','确定要从数据库删除所选记录?',function(btn,text){
						if(btn == 'yes'){
							var num = record.get('num');
							Ext.Ajax.request( {
								url :'../'+WebServiceUrl,
								params : {
									trancode :'51217',
									head:num
								},
								method :'Post',
								success : function(result, request) {
									var jsonData = Ext.util.JSON.decode(result.responseText);
									if (jsonData.success) {
										Ext.MessageBox.alert('操作成功', jsonData.message);
										stockRecordGrid.getStore().reload();
									} else {
										Ext.MessageBox.alert('操作失败', jsonData.message);
									}
								},
								failure : function(result, request) {
									Ext.MessageBox.alert('操作失败', '联系管理员');
								}
							});	
						}
					});
				}else{
					Ext.MessageBox.alert('操作错误','请先选择一条记录');
				}
			}
		}],
		bbar: new Ext.PagingToolbar({
            store: storeRecord,
            pageSize:App.pageSize,
            displayInfo: true,
            displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
        	emptyMsg: "没有记录"
        }),
        viewConfig:viewConfig,
		loadMask: true
    });
    
    stockRecordGrid.on('rowclick',function(Grid, rowIdx){
    	var num = Grid.getSelectionModel().getSelected().get('num');
    	var store = Ext.getCmp('gridDetail').getStore();
		store.baseParams = {
			num : num,
			trancode : '51213'
		};
		store.reload();
    	Ext.getCmp('materielGrid').show();
 		Ext.getCmp('viewport').doLayout();
    });
    storeRecord.load({params:{start:0, limit:App.pageSize}});
	
    
    //记录明细grid
    var cmDtail = new Ext.grid.ColumnModel([sm,
		{header : '单据编号',    dataIndex :'num',	hidden:true},
		{header : '物料名称',    dataIndex :'materielName'},
		{header : '批号',    dataIndex :'batchno'},
		/*{header : '采购类型',    dataIndex :'kind',		
			renderer:function(v){
				if(v==0)return '普通发货'
				else if(v==1)return '特殊发货'
				else if(v==2)return '紧急发货'
				else if(v==3)return '新品发货'
				else if(v==4)return '售后退回'
				else if(v==5)return '退货入库'
				else if(v==6)return '配件入库'
			}
		},
		{header : '出库类型',    dataIndex :'type',
			renderer:function(v){
				if(v==0)return '普通出库'
				else if(v==1)return '门店退回入库'
			}
		},*/
		{header : '对应单据',    dataIndex :'ref_num',	hidden:true},
		{header : '对应序号',    dataIndex :'ref_ord',	hidden:true},
		{header : '机身码',    dataIndex :'serialNo',hidden:true},
		{header : '厂家条码',    dataIndex :'factcode',hidden:true},
		{header : 'OTL条码',    dataIndex :'otlcode',hidden:true},
		{header : '批次',    dataIndex :'batchid',hidden:true},
		{header : '出库库位',    dataIndex :'pgid',	hidden:true},
		{header : '出库数量',    dataIndex :'amount'},
		{header : '含税价格',    dataIndex :'in_price'},
		{header : '合计',		dataIndex : 'total'},
		{header : '税额',    dataIndex :'tax_money',hidden:true},
		{header : '零售价',    dataIndex :'price',hidden:true},
		{header : '批发价',    dataIndex :'out_price',hidden:true},
		{header : '生产日期',    dataIndex :'prod_date',renderer : Ext.util.Format.dateRenderer('Y-m-d'),hidden:true},
		{header : '有效期',    dataIndex :'end_date',renderer : Ext.util.Format.dateRenderer('Y-m-d'),	hidden:true},
		{header : '保修期',    dataIndex :'rep_date',renderer : Ext.util.Format.dateRenderer('Y-m-d'),	hidden:true},
		{header : '质量',    dataIndex :'quality',	hidden:true},
		{header : '合格证',    dataIndex :'okpass',	hidden:true},
		{header : '随货同行联',    dataIndex :'inno',	hidden:true},
		{header : '发票',    dataIndex :'infp',	hidden:true},
		{header : '财务复核标记',    dataIndex :'cwaf',	hidden:true},
		{header : '财务复核日期',    dataIndex :'cwaf_date',renderer : Ext.util.Format.dateRenderer('Y-m-d'),	hidden:true},
		{header : '财务复核人员',    dataIndex :'cwaf_man',	hidden:true},
		{header : '财务复核付款日期',    dataIndex :'cwaf_pay_date',renderer : Ext.util.Format.dateRenderer('Y-m-d'),	hidden:true},
		{header : '财务复核付款人员',    dataIndex :'cwaf_pay_man',	hidden:true},
		{header : '财务已复核付款累计数量',    dataIndex :'cwaf_amount',	hidden:true},
		{header : '对应付款ARAP流水',    dataIndex :'pay_arap',	hidden:true},
		{header : '出库审核人员',    dataIndex :'apid',	hidden:true},
		{header : '出库审核日期',    dataIndex :'adate',renderer : Ext.util.Format.dateRenderer('Y-m-d'),	hidden:true},
		{header : '出库状态',    dataIndex :'state',	hidden:true},
		{header : '审核状态',    dataIndex :'ischeck',	hidden:true},
		{header : '备注',    dataIndex :'remark'},
		{header : '财务单价',    dataIndex :'cw_inprice',	hidden:true},
		{header : '财务税额',    dataIndex :'cw_taxmoney',	hidden:true},
		{header : '财务备注',    dataIndex :'cw_remark',	hidden:true},
		{header : '财务记帐日期',    dataIndex :'ref_date',renderer : Ext.util.Format.dateRenderer('Y-m-d'),	hidden:true},
		{header : '财务状态',    dataIndex :'cw_state',	hidden:true}
	]);
	cmDtail.defaultSortable = true;
	
    var storeDetail = new Ext.data.Store({
		
		proxy : new Ext.data.HttpProxy({
			url : '../' + WebServiceUrl
		}),
		reader : new Ext.data.JsonReader({
			totalProperty : 'totalProperty',
			root : 'root'
		},  
		[{name:'num'},
		{name:'ord'},
		{name:'gid'},
		{name:'batchno'},
		{name:'kind'},
		{name:'type'},
		{name:'ref_num'},
		{name:'ref_ord'},
		{name:'serialNo'},
		{name:'factcode'},
		{name:'otlcode'},
		{name:'batchid'},
		{name:'pgid'},
		{name:'amount'},
		{name:'in_price'},
		{name:'tax_money'},
		{name:'price'},
		{name:'out_price'},
		{name:'prod_date',convert:dateConv},
		//{name:'end_date',convert:dateConv},
		//{name:'rep_date',convert:dateConv},
		{name:'quality'},
		{name:'okpass'},
		{name:'inno'},
		{name:'infp'},
		{name:'cwaf'},
		//{name:'cwaf_date',convert:dateConv},
		{name:'cwaf_man'},
		//{name:'cwaf_pay_date',convert:dateConv},
		{name:'cwaf_pay_man'},
		{name:'cwaf_amount'},
		{name:'pay_arap'},
		{name:'apid'},
		//{name:'adate',convert:dateConv},
		{name:'state'},
		{name:'ischeck'},
		{name:'remark'},
		{name:'cw_inprice'},
		{name:'cw_taxmoney'},
		{name:'cw_remark'},
		//{name:'ref_date',convert:dateConv},
		{name:'cw_state'},
		{name:'materielName'},
		{name:'total'}
		])
    });
    
   var gridDetail = new Ext.grid.GridPanel({
    	title:'订单明细',
    	id:'gridDetail',
		autoScroll: true,
		store: storeDetail,
		cm: cmDtail,
		sm: new Ext.grid.CheckboxSelectionModel({singleSelect : true}),
		border:false,
		viewConfig:viewConfig,
		loadMask: true
    });
    
    
	var viewport = new Ext.Viewport({
        layout:'border',
        id:'viewport',
        split:true,
		items:[{
			split:true,
			collapsible:true,
			border:false,
			region:'center',
			layout:'fit',
			items:stockRecordGrid
		},{
			region:'south',
			layout:'fit',
			id:'materielGrid',
			border:false,
			hidden:true,
			height:150,

			items : [new Ext.TabPanel({
	    		tabPosition:'bottom',
				activeTab:0,
				enableTabScroll:true,
				items:gridDetail
			})]
		}]
	});
	
	Ext.getCmp('condition').on('specialkey', function(field, event){//注册回车事件
        if(event.getKey() == event.ENTER){
			doSearch();
        }
    })
	
	function doSearch(){
		var condition = Ext.get('condition').getValue();//取得搜索条件
		
		storeRecord.baseParams = {
			condition : condition,
			trancode : '51212'
		};
		storeRecord.load({params : {start : 0,limit : App.pageSize}});
	}
})